package aapt

// Aapt represents aapt util
type Aapt struct {
	binPath string
}

// AppInfo represents application's info
type AppInfo struct {
	PackageName           string
	VersionName           string
	VersionCode           int
	MinSDKVersion         int
	TargetSDKVersion      int
	UsesAndroidPermission []string          // like android.permission.USE_FINGERPRINT
	UsesCustomPermission  []string          // like com.htc.launcher.permission.UPDATE_SHORTCUT
	LaunchActivity        []string          // it is possible have more than one launch activity
	AppLabel              map[string]string // NoLocale and ru-RU and so on
	MetaData              map[string]string // for example: "meta-data: name='com.android.vending.splits' resource='res/xml/splits0.xml'"
	IsSplitBase           bool              // when "meta-data: name='com.android.vending.splits.required'" presents
	Debuggable            bool
	TestOnly              bool
	NativeCode            map[string]struct{} // Empty struct takes 0 bytes in memory
}

// Resources represents application's resources
type Resources struct {
	// android:array
	Array map[string][][2]string
	// android:string
	String map[string][][2]string
}

// BinPath returns path to aapt's bin file
func (aapt Aapt) BinPath() string {
	return aapt.binPath
}

// XmlStringsManifest returns dump for strings of AndroidManifest.xml
func (aapt Aapt) XmlStringsManifest(apkFile string) ([]byte, error) {
	cmd := []string{"xmlstrings", apkFile, "AndroidManifest.xml"}
	return aapt.RunSync("dump", cmd...)
}

// removeFilesFrom removes files from apk files
func (aapt Aapt) removeFilesFrom(apkAarJar string, files ...string) ([]byte, error) {
	return aapt.RunSync("r", append([]string{apkAarJar}, files...)...)
}

// RemoveSignature removes apk's V1 signature
func (aapt Aapt) RemoveSignature(zipFile string) ([]byte, error) {
	sign, err := findApkSignature(zipFile)
	if err != nil {
		return nil, err
	}
	return aapt.removeFilesFrom(zipFile, sign...)
}

// DumpAppInfo returns raw app info dump. See ParseAppInfoDumpFromBytes
func (aapt Aapt) DumpAppInfo(apkFile string) ([]byte, error) {
	return aapt.RunSync("dump", "--include-meta-data", "badging", apkFile)
}

// DumpResources returns raw app resources dump. See ParseDumpResourcesFromBytes
func (aapt Aapt) DumpResources(apkFile string) ([]byte, error) {
	return aapt.RunSync("dump", "--values", "resources", apkFile)
}

// Version returns aapt's version
func (aapt Aapt) Version() ([]byte, error) {
	return aapt.RunSync("version")
}
